package org.byteDance.l082;

/**
 * @Author yu
 * @Date 2022/3/7
 */
public class Solution {

    public static void main(String[] args) {

        ListNode listNode = new ListNode(5);
        ListNode listNode2 = new ListNode(4, listNode);
        ListNode listNode3 = new ListNode(4, listNode2);
        ListNode listNode4 = new ListNode(3, listNode3);
        ListNode listNode5 = new ListNode(3, listNode4);
        ListNode listNode6 = new ListNode(2, listNode5);
        ListNode listNode7 = new ListNode(1, listNode6);

        Solution solution = new Solution();
        ListNode node = solution.deleteDuplicates4(listNode7);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }

    }

    // 1,2,3,3,4,4,5
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        // 判断 head 是否与下一个节点相等
        if (head.val != head.next.val) {
            head.next = deleteDuplicates(head.next);
            return head;
        } else {

            ListNode next = head.next.next;

            // 如果 head 节点与下一个节点相等，找到下一个节点的下一个节点，head 节点与其进行比较
            while (next != null && head.val == next.val) {
                next = next.next;
            }

            return deleteDuplicates(next);
        }

    }

    public ListNode deleteDuplicates2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        if (head.val != head.next.val) {
            head.next = this.deleteDuplicates2(head.next);
            return head;
        } else {
            ListNode node = head.next.next;

            while (node != null &&  node.val == head.val) {
                node = node.next;
            }
            return this.deleteDuplicates2(node);
        }
    }

    public ListNode deleteDuplicates3(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        if (head.val != head.next.val) {
            head.next = deleteDuplicates3(head.next);
            return head;
        } else {
            /*ListNode node = head.next.next;
            while (node != null && node.val == head.val) {
                node = node.next;
            }*/
            ListNode node = head;
            while (node != null && node.val == node.next.val) {
                node = node.next;
            }
            return deleteDuplicates3(node);
        }
    }

    public ListNode deleteDuplicates4(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        if (head.val != head.next.val) {
            head.next = deleteDuplicates4(head.next);
            return head;
        } else {
            ListNode node = head.next.next;
            while (node != null && node.val == head.val) {
                node = node.next;
            }

            return deleteDuplicates4(node);
        }
    }

}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}